﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.DirectoryServices;
using System.Text;

namespace umsoftU3.Common
{
    /// <summary>
    ///ADOperation 的摘要说明(AD 操作类)
    /// </summary>
    public class ADOperation
    {
        public ADOperation()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 获得DirectoryEntry对象实例,以管理员登陆AD
        /// </summary>
        /// <returns></returns>
        private static DirectoryEntry GetDirectoryObject()
        {
            DirectoryEntry entry = null;
            try
            {
                entry = new DirectoryEntry("LDAP://xunyisoft.com", "xunyisoft\\gary", "password!1", AuthenticationTypes.Secure);
            }
            catch (Exception ex)
            {
                //OThinker.H3.Server.Engine.LogWriter.Write("获取AD对象实例失败！【GetDirectoryObject】");
            }
            return entry;
        }

        /// <summary>
        /// 判断用户是否存在
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public static bool UserExists(string UserName)
        {
            try
            {
                DirectoryEntry entry = GetDirectoryObject();
                DirectorySearcher Search = new DirectorySearcher();
                Search.SearchRoot = entry;
                Search.Filter = "(&(objectClass=user) (cn=" + UserName + "))";
                SearchResultCollection results = Search.FindAll();
                if (results.Count == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                string Str = ex.Message;
                //OThinker.H3.Server.Engine.LogWriter.Write(Str + "[UserExists]");
                return false;
            }
        }

        /// <summary>
        /// 增加AD用户(Add AD User)
        /// </summary>
        /// <param name="vAccount"></param>
        /// <param name="userName"></param>
        /// <param name="vEmail"></param>
        /// <param name="vCompanyName"></param>
        /// <param name="vDeptName"></param>
        /// <param name="vTitle"></param>
        /// <returns></returns>
        public static bool AddDirectoryEntry(string vAccount, string userName, string vEmail, string vCompanyName, string vDeptName, string vTitle)
        {
            try
            {
                DirectoryEntry myEntry = GetDirectoryObject();
                DirectoryEntries myEntries = myEntry.Children;
                string Strname = "CN=" + vAccount;
                DirectoryEntry myDirectoryEntry = myEntries.Add(Strname, "user");
                //myDirectoryEntry.Properties["sAMAccountName"].Value = vAccount;
                myDirectoryEntry.Properties["displayName"].Value = userName;
                myDirectoryEntry.Properties["mail"].Value = vEmail;
                myDirectoryEntry.Properties["company"].Value = vCompanyName;
                myDirectoryEntry.Properties["department"].Value = vDeptName;
                myDirectoryEntry.Properties["title"].Value = vTitle;

                myDirectoryEntry.CommitChanges();
                myDirectoryEntry.Invoke("SetPassword", new object[] { "password@1" });
                return true;
            }
            catch (Exception ex)
            {
                //OThinker.H3.Server.Engine.LogWriter.Write("添加用话失败，原因：" + ex.Message + "[UserExists]");
                return false;
            }


        }


    }
}